package com.centaline.androidsalesblog.activities;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.net.Uri;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.centaline.androidsalesblog.R;
import com.centaline.androidsalesblog.base.BaseActivity;
import com.centaline.androidsalesblog.constants.ExtraConstant;
import com.centaline.androidsalesblog.widgets.MLoadView;
import com.centanet.framework.widget.AbstractLoadView;

import java.util.Locale;

/**
 * web页面
 * Created by guilin on 16/3/1.
 */
public class WebActivity extends BaseActivity {

    private MLoadView mLoadView;
    private WebView webView;

    private String url;

    @Override
    protected int setLayoutId() {
        return R.layout.activity_web;
    }

    @Override
    protected void findViews() {
        setUniversalToolbar();
        mLoadView = (MLoadView) findViewById(R.id.mLoadView);
        webView = (WebView) findViewById(R.id.webView);
    }

    @SuppressLint("SetJavaScriptEnabled")
    @Override
    protected void initViews() {
        mLoadView.setLoadStatusListener(new AbstractLoadView.LoadStatusListener() {
            @Override
            public void load() {
                loadUrl();
            }
        });
        final WebSettings webSettings = webView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webSettings.setUseWideViewPort(true);
        webSettings.setLoadWithOverviewMode(true);
        webSettings.setDefaultTextEncodingName("UTF-8");

        webView.setWebViewClient(new WebViewClient() {

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                if (url.startsWith("tel:")) {
                    startActivity(new Intent(Intent.ACTION_DIAL, Uri.parse(url)));
                    return true;
                } else {
                    return super.shouldOverrideUrlLoading(view, url);
                }
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                mLoadView.loadSuccess();
                super.onPageFinished(view, url);
            }
        });

        webView.setWebChromeClient(new WebChromeClient() {

            @Override
            public void onReceivedTitle(WebView view, String title) {
                if (title.equals("找不到网页")) {
                    snack("网络错误");
                    webView.setVisibility(View.GONE);
                } else {
                    setToolbarTitle(TextUtils.isEmpty(title) ? "网页" : title);
                }
                super.onReceivedTitle(view, title);
            }

        });

        url = getIntent().getStringExtra(ExtraConstant.WebUrl);
        loadUrl();
    }

    private void loadUrl() {
        webView.loadUrl(url);
    }

    @Override
    protected void onDestroy() {
        webView.destroy();
        super.onDestroy();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_share, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home &&
                webView.canGoBack()) {
            webView.goBack();
            return true;
        } else if (R.id.action_share == item.getItemId()) {
            Intent intent = new Intent(Intent.ACTION_SEND);
            intent.setType("text/plain");
            intent.putExtra(Intent.EXTRA_TEXT, String.format(Locale.CHINA, "%s,请访问%s",
                    webView.getTitle(),
                    webView.getUrl()));
            startActivity(intent);
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (event.getAction() == KeyEvent.ACTION_DOWN &&
                webView.canGoBack()) {
            webView.goBack();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}
